package com.project.fortuneteller.service.impl;

import cn.hutool.db.Page;
import com.project.fortuneteller.common.CommonEnum;
import com.project.fortuneteller.common.DivineSearchPage;
import com.project.fortuneteller.common.QueryPageSearchBean;
import com.project.fortuneteller.mappers.DivinatoryDetailsMapper;
import com.project.fortuneteller.mappers.LearnBookMapper;
import com.project.fortuneteller.mappers.LearnContentMapper;
import com.project.fortuneteller.pojo.Content;
import com.project.fortuneteller.pojo.DivinatoryDetails;
import com.project.fortuneteller.pojo.vo.LearnContentVO;
import com.project.fortuneteller.service.LearnService;
import com.project.fortuneteller.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 学习板块
 */
@Slf4j
@Service
public class LearnServiceImpl implements LearnService {
    // 获取 学习书籍
    @Autowired
    private LearnBookMapper learnBookMapper;

    // 获取 学习书籍内容
    @Autowired
    private LearnContentMapper learnContentMapper;

    // 模糊匹配 实现 详细信息 的 匹配
    @Autowired
    private DivinatoryDetailsMapper divinatoryDetailsMapper;

    /**
     * 获取书籍的全部文章 信息
     *
     * @param name 书籍名称
     * @return
     */
    @Override
    public Result getBookContents(String name) {
        String res = learnBookMapper.selectIdByName(name);
        List<Content> contents = learnContentMapper.selectByBookId(res);
        if (contents == null) {
            return Result.error("查询失败 获取图书文件信息失败");
        }
        //查找成功
        return Result.success(contents);
    }




    /**
     * 通过 id 获取 content信息
     *
     * @param id
     * @return
     */

    @Override
    public Result getContentById(String id) {

        // 获取文章信息
        List<Content> contents = learnContentMapper.selectById(id);
        //
        if (contents == null) {
            return Result.error("文章内容不存在");
        }
        // 获取文章内容
        return Result.success(contents);
    }


    /**
     * 搜索模块使用的  sql 语法
     * @param queryPageSearchBean
     * @return
     */
    @Override
    public Result searchLike(QueryPageSearchBean queryPageSearchBean) {
        // 获取文章信息
        String queryLike = "%"+queryPageSearchBean.getContent()+"%";
        // 文章内容
        List<Content> contents = learnContentMapper.searchLikeContent(queryLike);
        // 卦象内容
       List<DivinatoryDetails> byLike = divinatoryDetailsMapper.getByLike(queryLike,(queryPageSearchBean.getCurrentPage()-1) *queryPageSearchBean.getPageSize() , queryPageSearchBean.getPageSize())    ;


//         if (contents == null) {
//            return Result.error("文章内容不存在");
//        }
//


        // 数据传输层
        LearnContentVO learnContentVO = new LearnContentVO();

        learnContentVO.setContent(contents);
        learnContentVO.setDivinatoryDetails(byLike);

        // 获取文章内容
        // 返回封装的内容
        return Result.success(learnContentVO);
    }
}
